package com.javaee.service.admin.impl;

import com.javaee.entity.Doctor;

import com.javaee.mapper.admin.DoctorMapper;
import com.javaee.service.admin.DoctorService;
import com.javaee.utils.AliOssUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class DoctorServiceImpl implements DoctorService {
    @Autowired
    private DoctorMapper doctorMapper;

    @Override
    public String toAddDoctor(Doctor doctor, Model model) {
        model.addAttribute("doctor", doctor);
        model.addAttribute("departments",doctorMapper.selectAllDepartments());
        return "admin/addDoctor";
    }

    @Override
    public String addDoctor(Doctor doctor, HttpServletRequest request) {
        MultipartFile file = doctor.getFileName();
        String originalFilename = file.getOriginalFilename();
        String fileUrl=null;
        try {
            fileUrl = AliOssUtil.uploadFile(originalFilename,file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
        doctor.setTx(fileUrl);
        doctorMapper.insertDoctor(doctor);
       return "redirect:/doctor/selectAllDoctorsByPage?currentPage=1";
    }

    @Override
    public String selectAllDoctorsByPage(Model model, int currentPage) {
        //共多少个医生
        int totalCount = doctorMapper.selectCount();
        System.out.println("totalCount = " + totalCount);
        //计算共多少页
        int pageSize = 5;
        int totalPage = (int)Math.ceil(totalCount*1.0/pageSize);
        List<Doctor> allDoctors = doctorMapper.selectAllDoctorsByPage((currentPage-1)*pageSize, pageSize);
        model.addAttribute("allDoctors", allDoctors);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currentPage", currentPage);

        return "admin/selectDoctors";
    }

    @Override
    public String detail(Model model, int id) {
        System.out.println( "医生id = " + id);
        Doctor doctor = doctorMapper.selectADoctor(id);
        
        System.out.println("查询的医生是doctor = " + doctor);
        model.addAttribute("doctor", doctor);
            //转向查看详情
            return "admin/detailDoctor";

    }

    @Override
    public String delete(int id) {
//         if(关于这个医生有挂号信息>0){
//             return "不能删除因为他有挂号信息";
//         }
         doctorMapper.deleteDoctor(id);
        return "删除成功";
    }

    @Override
    public String toUpdate(Model model, Integer id) {
        model.addAttribute("doctor", doctorMapper.selectADoctor(id));
        model.addAttribute("departments", doctorMapper.selectAllDepartments());
        return "admin/updateDoctor";
    }


    @Override
    public String update(Doctor doctor,String people,HttpServletRequest request) throws Exception {
        MultipartFile file = doctor.getFileName();
        if(!file.isEmpty()) {
            String originalFilename = file.getOriginalFilename();
            String fileUrl = AliOssUtil.uploadFile(originalFilename, file.getInputStream());
            doctor.setTx(fileUrl);
        }
        doctorMapper.updateDoctor(doctor);
        if(people!=null) {
            if (people.equals("d"))
                return "redirect:/doctorInfo/toDoctorInfo";
        }
        return "redirect:/doctor/selectAllDoctorsByPage?currentPage=1";
    }
}
